package com.shilan.administrator.qintfm.fragment.tablayoutFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.shilan.administrator.qintfm.MainActivity;
import com.shilan.administrator.qintfm.R;
import com.shilan.administrator.qintfm.adapter.AnchorAdapter;
import com.shilan.administrator.qintfm.beans.AnchorResult;
import com.shilan.administrator.qintfm.fragment.BaseFragment;
import com.shilan.administrator.qintfm.volley.VolleySingleton;

import java.util.List;

import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;

/**
 * Created by dllo on 16/1/12.
 */
public class AnchorFragment extends BaseFragment {
    private RecyclerView anchorRv;
    private AnchorAdapter anchorAdapter;
    private String url = "http://mobile.ximalaya.com/m/explore_user_index?device=android&page=1";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_anthor,null);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        anchorRv = (RecyclerView) view.findViewById(R.id.rv_anchor);
        anchorRv.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        anchorAdapter = new AnchorAdapter(getActivity());
        anchorRv.setAdapter(anchorAdapter);

        /**
         * 设置底部播放栏是否隐藏
         * **/
        anchorRv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    // 滑动时
                    case ACTION_MOVE:
                        MainActivity mainActivity = (MainActivity) getActivity();
                        // Visibility() MainActivity 中隐藏方法
                        mainActivity.Gone();
                        break;
                    // 手指移开时
                    case ACTION_UP:
                        MainActivity mActivity = (MainActivity) getActivity();
                        // MainActivity 中再次显示方法
                        mActivity.Visible();
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();
    }

    private void initData() {
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        AnchorResult result = gson.fromJson(response,
                                AnchorResult.class);
                        if (result != null) {
                            List<AnchorResult.ListEntity> anchorListEntity = result.getList();
                            if (anchorListEntity != null && anchorListEntity.size() > 0) {
                                anchorAdapter.addAnthorData(anchorListEntity);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }

    }

